﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using ReportWizard.DataAccess;
using ReportWizard.Helpers;
using ReportWizard.Interface;
using ReportWizard.Properties;

namespace ReportWizard.Forms
{
    public partial class SelectForm : IconForm
    {
        #region properties

        public string TargetText { get; private set; }

        private IBookmark CurrentBookmark { get; set; }

        private int SelectedObjectId { get; set; }

        #endregion

        #region ctor

        public SelectForm(IBookmark currentBookmark)
        {
            CurrentBookmark = currentBookmark;
            InitializeComponent();

            ShowCustomRightButton = false;
            ShowCheckFooter = false;
        }

        #endregion

        private void FillSelectForm()
        {
            dgvMembers.DataSource = null;
            comboMembers.DataSource = null;

            dgvMembers.AutoGenerateColumns = false;
            if (CurrentBookmark != null)
            {
                try
                {

                    List<ReportBaseObject> rList = ReportBaseObject.Select(true, CurrentBookmark.Id, CurrentBookmark is BookmarkGroup);
                    if (rList != null)
                    {
                        dgvMembers.DataSource = rList;
                        comboMembers.DataSource = rList;
                        comboMembers.DisplayMember = "Name";
                        comboMembers.ValueMember = "Id";
                    }
                }
                catch (Exception ex)
                {
                    Logger.WriteLog(ex);
                }
            }
        }

        private void SelectFormLoad(object sender, EventArgs e)
        {
            Text = string.Format("Список значений для поля \"{0}\"", Globals.TargetFormProfile);
            FillSelectForm();
        }

        private void SelectObjectClick(object sender, EventArgs e)
        {
            if (dgvMembers.SelectedRows.Count != 0)
            {
                TargetText = dgvMembers.SelectedRows[0].Cells["cName"].Value.ToString();
                DialogResult = DialogResult.OK;
            }
        }

        private void CreateObjectClick(object sender, EventArgs e)
        {
            CreateBaseObjectForm createEntityForm = new CreateBaseObjectForm(CurrentBookmark);
            createEntityForm.ShowDialog(this);
        }

        private void DeleteObjectClick(object sender, EventArgs e)
        {
            if (SelectedObjectId > 0)
            {
                if (
                    MessageBox.Show(string.Format("Удалить \"{0}\"?", ReportBaseObject.Select(SelectedObjectId).Name),
                                    Resources.Deleting, MessageBoxButtons.OKCancel, MessageBoxIcon.Question,
                                    MessageBoxDefaultButton.Button1) == DialogResult.OK)
                {
                    if (ReportBaseObject.Delete(SelectedObjectId))
                        FillSelectForm();
                }
            }
        }


        private void DgvMembersCellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            TargetText = dgvMembers.Rows[e.RowIndex].Cells["cName"].Value.ToString();
            DialogResult = DialogResult.OK;
        }

        private void CheckShowAllCheckedChanged(object sender, EventArgs e)
        {
            Globals.OnlyVisibleItems = !CheckFooterChecked;
            FillSelectForm();
        }

        private void DgvMembersRowEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (dgvMembers.Rows[e.RowIndex].Cells["cId"].Value != null)
            {
                SelectedObjectId = Convert.ToInt32(dgvMembers.Rows[e.RowIndex].Cells["cId"].Value);
            }
        }

        private void CancelClick(object sender, EventArgs e)
        {
            Close();
        }
    }
}
